#pragma once
#include <iostream>
#include <time.h>
#include <thread>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <cstdarg>
#include <sstream>
#include <mutex>

namespace mycode
{
    #define max_logsize 1024

    using std::string;
    using std::thread;
    using std::cout;
    using std::endl;
    using std::mutex;

    #define LOG_DEBUG(format, ...)  Logger::Instance().log(LogLevel::DEBUG, __LINE__, format, ##__VA_ARGS__);
    #define LOG_INFO(format, ...)  Logger::Instance().log(LogLevel::INFO, __LINE__, format, ##__VA_ARGS__);
    #define LOG_WARN(format, ...)  Logger::Instance().log(LogLevel::WARN, __LINE__, format, ##__VA_ARGS__);
    #define LOG_ERROR(format, ...)  Logger::Instance().log(LogLevel::ERROR, __LINE__, format, ##__VA_ARGS__);
    #define LOG_FATAL(format, ...)  Logger::Instance().log(LogLevel::FATAL, __LINE__, format, ##__VA_ARGS__);

    enum LogLevel
    {
        DEBUG = 0,
        INFO,
        WARN,
        ERROR,
        FATAL
    };

    class Logger
    {
    public:
        static Logger& Instance();//单例对象

        //日志格式
        void log(LogLevel level, int line, const char* format, ...);
        //日志输出控制
        void setOutputFile(const string& fileName, bool append = true);
        void setOutputConsole(bool enable = true);

    private:
        //禁止创建对想
        Logger();
        ~Logger();
        Logger(const Logger&) = delete;
        Logger operator=(const Logger&) = delete;

    private:
        string _fileName;           //记录日志的文件名
        int _fileId;                //记录文件id
        bool _enableOutputConsole;  //是否在中断输出

        pthread_mutex_t _mtx;       //加锁
    };
}